package ooday03;

public class PolymorphicDemo {
    public static void main(String[] args) {
//演示向上造型（多态）的第二点应用
        //演示强转成功的条件
        Animal o =new Dog("小黑",2,"黑");//向上造型
        Dog d = (Dog)o;//引用o所指向的对象，就是Dog类型
        Swim s = (Swim)o;
       // Fish f= (Fish)o;//运行时会发生classcastException类型转换异常
        System.out.println(o instanceof Dog);//true
        System.out.println(o instanceof Swim);//true
        System.out.println(o instanceof Fish);//false
        if(o instanceof Fish){//false
            Fish f=(Fish)o;
        }






       //Animal animal=new Animal();编译错误，抽象类不能被实例化
        Animal[]animals=new Animal[5];
        animals[0]=new Dog("小黑",2,"黑");//向上造型
        animals[1]=new Dog("小黄",1,"黄");
        animals[2]=new Chick("小花",2,"花");
        animals[3]=new Chick("小灰",3,"灰");
        animals[4]=new Fish("小金",2,"金");
        for(int i=0;i<animals.length;i++){//遍历所有动物
            System.out.println(animals[i].name);//所有动物的名字
            animals[i].eat();//所有动物吃饭
            animals[i].drink();
            if(animals[i] instanceof Dog){
                Dog dog=(Dog)animals[i];
                dog.lookHome();
            }
            if(animals[i] instanceof Chick){
                Chick chick=(Chick)animals[i];
                chick.layEgg();
            }
            if(animals[i] instanceof Swim){
                Swim swim=(Swim)animals[i];
                swim.swim();
            }



        }







      /*  //演示向上造型的语法：
        Dog o1=new Dog("小黑",2,"黑");//狗是狗
        //o1.name/age/color/eat/drink/lookHome/swim
        Animal o2=new Dog("小黑",2,"黑");//狗是动物
        //o2.name/age/color/eat/drink
        Swim o3=new Dog("小黑",2,"黑");//狗会游泳
        //o3.swim();
    }*/
//演示向上造型（多态）第一点应用
       /* Master master=new Master();
        Animal dog=new Dog("小黑",2,"黑");
        Animal chick=new Chick("小白",2,"白");
        Animal fish=new Fish("小金",1,"金");
        master.feed(dog);
        master.feed(chick);
        master.feed(fish);*/
}
    }
